<HTML><HEAD><TITLE>/home/steder/Projects/Tutorials/python/BobChat-v0.3/BobClient.py</TITLE></HEAD>
                  <BODY BGCOLOR=#FFFFFF>
                  <!--header-->
                  <!--script--><PRE><FONT COLOR=#1111CC>#!/usr/bin/env python</FONT>
<FONT COLOR=#1111CC># pygame + PyOpenGL version of Nehe's OpenGL lesson10</FONT>
<FONT COLOR=#1111CC># Paul Furber 2001 - m@verick.co.za</FONT>

<FONT COLOR=#3333CC><B>import</B></FONT> os, random
<FONT COLOR=#3333CC><B>from</B></FONT> string <FONT COLOR=#3333CC><B>import</B></FONT> split
<FONT COLOR=#3333CC><B>from</B></FONT> math <FONT COLOR=#3333CC><B>import</B></FONT> sin, cos
<FONT COLOR=#3333CC><B>from</B></FONT> OpenGL.GL <FONT COLOR=#3333CC><B>import</B></FONT> *
<FONT COLOR=#3333CC><B>from</B></FONT> OpenGL.GLU <FONT COLOR=#3333CC><B>import</B></FONT> *
<FONT COLOR=#3333CC><B>import</B></FONT> pygame, pygame.image, pygame.key
<FONT COLOR=#3333CC><B>from</B></FONT> pygame.locals <FONT COLOR=#3333CC><B>import</B></FONT> *


textures = []
filter = 0
tris = []

yrot = 0.0
xpos = 0.0
zpos = 0.0

lookupdown = 0.0
walkbias = 0.0
walkbiasangle = 0.0

LightAmbient  = [ 0.5, 0.5, 0.5, 1.0]
LightDiffuse  = [ 1.0, 1.0, 1.0, 1.0]
LightPosition = [ 0.0, 0.0, 2.0, 1.0]

piover180 = 0.0174532925

<FONT COLOR=#3333CC><B>def</B></FONT><A NAME="resize"><FONT COLOR=#CC0000><B> resize</B></FONT></A>((width, height)):
    <FONT COLOR=#3333CC><B>if</B></FONT> height==0:
        height=1.0
    glViewport(0, 0, width, height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45, float(width)/height, 0.1, 100.0)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

<FONT COLOR=#3333CC><B>def</B></FONT><A NAME="init"><FONT COLOR=#CC0000><B> init</B></FONT></A>():
    <FONT COLOR=#3333CC><B>global</B></FONT> lookupdown, walkbias, walkbiasangle
    
    glEnable(GL_TEXTURE_2D)
    load_textures()
    glShadeModel(GL_SMOOTH)
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glClearDepth(1.0)
    glEnable(GL_DEPTH_TEST)
    glDepthFunc(GL_LEQUAL)
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
    glLightfv( GL_LIGHT1, GL_AMBIENT, LightAmbient )
    glLightfv( GL_LIGHT1, GL_DIFFUSE, LightDiffuse )
    glLightfv( GL_LIGHT1, GL_POSITION, LightPosition )
    glEnable( GL_LIGHT1 )
    lookupdown    = 0.0
    walkbias      = 0.0
    walkbiasangle = 0.0
    glColor4f( 1.0, 1.0, 1.0, 0.5)


<FONT COLOR=#3333CC><B>def</B></FONT><A NAME="load_textures"><FONT COLOR=#CC0000><B> load_textures</B></FONT></A>():
    <FONT COLOR=#3333CC><B>global</B></FONT> textures
    
    texturefile = os.path.join(<FONT COLOR=#115511>'data'</FONT>,<FONT COLOR=#115511>'mud.bmp'</FONT>)
    textureSurface = pygame.image.load(texturefile)
    textureData = pygame.image.tostring(textureSurface, <FONT COLOR=#115511>"RGBX"</FONT>, 1)

    textures = glGenTextures(3)
    
    glBindTexture(GL_TEXTURE_2D, textures[0])
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, textureSurface.get_width(), textureSurface.get_height(), 0,
                  GL_RGBA, GL_UNSIGNED_BYTE, textureData )
    glBindTexture(GL_TEXTURE_2D, textures[1])
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, textureSurface.get_width(), textureSurface.get_height(), 0,
                  GL_RGBA, GL_UNSIGNED_BYTE, textureData )
    glBindTexture(GL_TEXTURE_2D, textures[2])
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, textureSurface.get_width(), textureSurface.get_height(), 0,
                  GL_RGBA, GL_UNSIGNED_BYTE, textureData )
    gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA, textureSurface.get_width(), textureSurface.get_height(),
                       GL_RGBA, GL_UNSIGNED_BYTE, textureData );

<FONT COLOR=#3333CC><B>def</B></FONT><A NAME="setup_world"><FONT COLOR=#CC0000><B> setup_world</B></FONT></A>():
    <FONT COLOR=#3333CC><B>global</B></FONT> tris
    verts = 0
    tri = []
    
    f = open(os.path.join(<FONT COLOR=#115511>"data"</FONT>, <FONT COLOR=#115511>"world.txt"</FONT>))
    lines = f.readlines()
    f.close()
    <FONT COLOR=#3333CC><B>for</B></FONT> line <FONT COLOR=#3333CC><B>in</B></FONT> lines:
        vals = split(line)
        <FONT COLOR=#3333CC><B>if</B></FONT> len(vals) != 5:
            <FONT COLOR=#3333CC><B>continue</B></FONT>
        <FONT COLOR=#3333CC><B>if</B></FONT> vals[0] == <FONT COLOR=#115511>'//'</FONT>:
            <FONT COLOR=#3333CC><B>continue</B></FONT>
        
        vertex = []
        <FONT COLOR=#3333CC><B>for</B></FONT> val <FONT COLOR=#3333CC><B>in</B></FONT> vals:
            vertex.append(float(val))
        tri.append(vertex)
        verts += 1
        <FONT COLOR=#3333CC><B>if</B></FONT> (verts == 3):
            tris.append(tri)
            tri = []
            verts = 0

<FONT COLOR=#3333CC><B>def</B></FONT><A NAME="draw"><FONT COLOR=#CC0000><B> draw</B></FONT></A>():
    <FONT COLOR=#3333CC><B>global</B></FONT> xpos, zpos, yrot, walkbias, lookupdown
    <FONT COLOR=#3333CC><B>global</B></FONT> textures, filter, tris

    xtrans = -xpos
    ztrans = -zpos
    ytrans = -walkbias-0.25
    sceneroty=360.0-yrot

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
    glLoadIdentity()
    glRotatef( lookupdown, 1.0, 0.0 , 0.0 )
    glRotatef( sceneroty, 0.0, 1.0 , 0.0 )
    glTranslatef( xtrans, ytrans, ztrans )
    glBindTexture( GL_TEXTURE_2D, textures[filter] )

    <FONT COLOR=#3333CC><B>for</B></FONT> tri <FONT COLOR=#3333CC><B>in</B></FONT> tris:
        glBegin(GL_TRIANGLES)
        glNormal3f( 0.0, 0.0, 1.0)
        
        glTexCoord2f(tri[0][3], tri[0][4])
        glVertex3f(tri[0][0], tri[0][1], tri[0][2])

        glTexCoord2f(tri[1][3], tri[1][4])
        glVertex3f(tri[1][0], tri[1][1], tri[1][2])

        glTexCoord2f(tri[2][3], tri[2][4])
        glVertex3f(tri[2][0], tri[2][1], tri[2][2])

        glEnd()

<FONT COLOR=#3333CC><B>def</B></FONT><A NAME="handle_keys"><FONT COLOR=#CC0000><B> handle_keys</B></FONT></A>(key):
    <FONT COLOR=#3333CC><B>global</B></FONT> xpos, zpos, yrot, filter
    <FONT COLOR=#3333CC><B>global</B></FONT> piover180, walkbiasangle, walkbias
    
    <FONT COLOR=#3333CC><B>if</B></FONT> key==K_ESCAPE:
        <FONT COLOR=#3333CC><B>return</B></FONT> 0

    <FONT COLOR=#3333CC><B>if</B></FONT> key==K_f:
        filter +=1
        <FONT COLOR=#3333CC><B>if</B></FONT> filter == 3:
            filter = 0
    <FONT COLOR=#3333CC><B>if</B></FONT> key==K_RIGHT:
        yrot -= 1.5
    <FONT COLOR=#3333CC><B>if</B></FONT> key==K_LEFT:
        yrot += 1.5
    <FONT COLOR=#3333CC><B>if</B></FONT> key==K_UP:
        xpos -= sin( yrot * piover180 ) * 0.05
        zpos -= cos( yrot * piover180 ) * 0.05
        <FONT COLOR=#3333CC><B>if</B></FONT> ( walkbiasangle &gt;= 359.0 ):
            walkbiasangle = 0.0
        <FONT COLOR=#3333CC><B>else</B></FONT>:
            walkbiasangle += 10.0
        walkbias = sin( walkbiasangle * piover180 ) / 20.0
    <FONT COLOR=#3333CC><B>if</B></FONT> key==K_DOWN:
        xpos += sin( yrot * piover180 ) * 0.05
        zpos += cos( yrot * piover180 ) * 0.05
        <FONT COLOR=#3333CC><B>if</B></FONT> ( walkbiasangle &lt;= 1.0 ):
            walkbiasangle = 359.0
        <FONT COLOR=#3333CC><B>else</B></FONT>:
            walkbiasangle -= 10.0
        walkbias = sin( walkbiasangle * piover180 ) / 20.0

    <FONT COLOR=#3333CC><B>return</B></FONT> 1

<FONT COLOR=#3333CC><B>def</B></FONT><A NAME="main"><FONT COLOR=#CC0000><B> main</B></FONT></A>():

    <FONT COLOR=#3333CC><B>global</B></FONT> surface

    video_flags = OPENGL|DOUBLEBUF
    
    pygame.init()
    surface = pygame.display.set_mode((640,480), video_flags)
    pygame.key.set_repeat(100,0)

    random.seed()
    resize((640,480))
    init()
    setup_world()

    frames = 0
    done = 0
    ticks = pygame.time.get_ticks()
    <FONT COLOR=#3333CC><B>while</B></FONT> <FONT COLOR=#3333CC><B>not</B></FONT> done:
        <FONT COLOR=#3333CC><B>while</B></FONT> 1:
            event = pygame.event.poll()
            <FONT COLOR=#3333CC><B>if</B></FONT> event.type == NOEVENT:
                <FONT COLOR=#3333CC><B>break</B></FONT>
            <FONT COLOR=#3333CC><B>if</B></FONT> event.type == KEYDOWN:
                <FONT COLOR=#3333CC><B>if</B></FONT> handle_keys(event.key) == 0:
                    done = 1
            <FONT COLOR=#3333CC><B>if</B></FONT> event.type == QUIT:
                done = 1
        
        draw()
        pygame.display.flip()
        frames += 1


    <FONT COLOR=#3333CC><B>print</B></FONT> <FONT COLOR=#115511>"fps:  %d"</FONT> % ((frames*1000)/(pygame.time.get_ticks()-ticks))


<FONT COLOR=#3333CC><B>if</B></FONT> __name__ == <FONT COLOR=#115511>'__main__'</FONT>: main()
</PRE>
                  <!--footer-->
                  </BODY>
